package com.sxhalo.fsms.util;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sxhalo.fsms.exception.BaseEnumError;

public class ApiResult implements Serializable {

	private static final long serialVersionUID = -7346151252609100793L;


	private Boolean success = true;
	/**
	 *	 错误码
	 */
	private String errorCode = null;

	/**
	 * 	错误信息
	 */
	private String errorMessage = null;

	/**
	 * 	响应内容，默认为null
	 */
	private Object data = null;
	
	private Object param = null;

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Object getParam() {
		return param;
	}

	public void setParam(Object param) {
		this.param = param;
	}

	/**
	 * 	无data的正常返回
	 */
	public static ApiResult retOK(HttpServletRequest req) {
		ApiResult response = new ApiResult();
		response.setParam(req.getAttribute("param"));
		req.setAttribute("returnData", response);
		return response;
	}

	/**
	 * 	有data的正常返回
	 *
	 * @param data data内容
	 * @param      <T> data类型
	 */
	public static ApiResult retOK(HttpServletRequest req, Object data) {
		ApiResult response = new ApiResult();
		response.setParam(req.getAttribute("param"));
		response.setData(data);
		req.setAttribute("returnData", response);
		return response;
	}

	/**
	 * 	无data的失败返回
	 *
	 * @param error 错误类型
	 */
	public static ApiResult retFail(BaseEnumError error) {
		return retFail(error.getCode(), error.getMsg());
	}

	/**
	 * 	有data的失败返回
	 *
	 * @param error 错误类型
	 * @param data  详细错误信息
	 */
	public static ApiResult retFail(BaseEnumError error, Object body) {
		return retFail(error.getCode(), error.getMsg(), body);
	}
	
	/**
	 *	 无data的失败返回
	 *
	 * @param code 错误码
	 * @param msg  错误信息
	 */
	public static ApiResult retFail(String code, String msg) {
		ApiResult response = new ApiResult();
		response.setSuccess(false);
		response.setErrorCode(code);
		response.setErrorMessage(msg);
		return response;
	}

	/**
	 * 	有data的失败返回 <br>
	 * 	失败返回的场景不多，所以没有严格要求T泛型
	 *
	 * @param code 错误码
	 * @param msg  错误信息
	 */
	public static ApiResult retFail(String code, String msg, Object data) {
		ApiResult response = new ApiResult();
		response.setSuccess(false);
		response.setErrorCode(code);
		response.setErrorMessage(msg);
		response.setData(data);
		return response;

	}
	
	public String toString(HttpServletRequest req) {
		ObjectMapper objectMapper = new ObjectMapper();
        // NULL不参与序列化
        objectMapper.setSerializationInclusion(Include.NON_NULL);
		try {
        	ApiResult response = this;
        	response.setParam(req.getAttribute("param"));
			return objectMapper.writeValueAsString(response);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return "";
    }
}
